// import { config as envConf } from './env';
import { urlParam } from './url-parser';
import axios from 'axios';
import CryptoJS from 'crypto-js';
import md5 from 'md5';
import qs from 'qs'

// 加密接口
axios.interceptors.request.use((config) => {
  let baseUrl = process.env.BASE_URL;
  config.url = baseUrl + config.url;
  const commonData = config.params;
  config.data = config.headers["Content-Type"] === "application/x-www-form-urlencoded" ? qs.stringify(commonData) : JSON.stringify(commonData);
  config.params = '';
  return config;


},
  (err) => {
    return Promise.resolve(err);
  }
);

// axios.interceptors.response.use((res) => res.data, (err) =>Promise.resolve(err));
axios.interceptors.response.use((res) => res.data, (err) => Promise.reject(err));

/**
 * function getMethod for for http request of method get
 *
 * @export
 * @param {string} url
 * @returns {AxiosPromise<any>}
 */
export function getMethod(url, config) {
  return axios({
    url,
    ...config,
    method: 'get',
  });
}

/**
 * function postMethod for for http request of method post
 *
 * @export
 * @param {string} url
 * @param {Object} params
 * @returns {AxiosPromise<any>}
 */
export function postMethod(url, config) {
  return axios({
    method: 'post',
    url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    ...config,
  });
}

/**
 * function putMethod for for http request of method put
 *
 * @export
 * @param {string} url
 * @param {Object} params
 * @returns {AxiosPromise<any>}
 */
export function putMethod(url, config) {
  return axios({
    url,
    ...config,
    method: 'put',
  });
}

/**
 * function deleteMethod for for http request of method delete
 *
 * @export
 * @param {string} url
 * @param {Object} params
 * @returns {AxiosPromise<any>}
 */
export function deleteMethod(url, config) {
  return axios({
    url,
    ...config,
    method: 'delete',
  });
}

/**
 * @description: 单独加密手机号方法
 * @param {string} phone
 * @return {string}
 */
export function encryption(phone) {
  const keyvalue = CryptoJS.enc.Utf8.parse('zxmd2013qazwsx12');
  const iv = CryptoJS.enc.Utf8.parse('qazwsx12zxmd2013');
  return CryptoJS.AES.encrypt(phone, keyvalue, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  }).toString() + '_Y';
}
